package com.imooc.news_manager_project.controller;

import com.imooc.news_manager_project.common.Result;
import com.imooc.news_manager_project.common.validator.SaveValidator;
import com.imooc.news_manager_project.common.validator.UpdateValidator;
import com.imooc.news_manager_project.dto.NewsDTO;
import com.imooc.news_manager_project.query.NewsQuery;
import com.imooc.news_manager_project.service.INewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "新闻管理")
@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private INewsService newsService;


    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token值", required = true)
    @ApiOperation(value = "保存新闻")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Validated(SaveValidator.class) NewsDTO newsDTO, HttpServletRequest httpServletRequest) {
        return newsService.save(request, newsDTO);
    }


    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", required = true)
    @ApiOperation(value = "根据id获取新闻数据")
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") String id) {
        return newsService.getById(id);
    }


    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", required = true)
    @ApiOperation(value = "条件查询新闻数据")
    @GetMapping("/list/{title}")
    public Result listByParams(@PathVariable("title") String title) {
        return newsService.listByParams(title);
    }

    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", required = true)
    @ApiOperation(value = "条件查询新闻数据")
    @PostMapping("/list/")
    public Result listByParams(@RequestBody NewsQuery newsQuery) {
        return newsService.listByParams(newsQuery);
    }

    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", required = true)
    @ApiOperation(value = "修改新闻数据")
    @PostMapping("/update")
    public Result update(HttpServletRequest request, @RequestBody @Validated(UpdateValidator.class) NewsDTO newsDTO) {
        return newsService.update(request, newsDTO);
    }

    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", required = true)
    @ApiOperation(value = "删除新闻数据")
    @DeleteMapping("/{id}")
    public Result remove(HttpServletRequest request, @PathVariable("id") String id) {
        return newsService.remove(request, id);
    }

    @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", required = true)
    @ApiOperation(value = "新闻数据点赞操作")
    @GetMapping("/likes/{id}")
    public Result likesById(HttpServletRequest request,@PathVariable("id") String id) {
        return newsService.likesById(request, id);
    }

}
